//
//  GameViewController.m
//  Load3dDemo
//
//  Created by hament on 2017/7/29.
//  Copyright © 2017年 Hament. All rights reserved.
//

/*检查字串*/
#define CHECKSTR(str) (str == nil || [str isKindOfClass:[NSNull class]])? @"" : str
#define TRANSTRATEY 1.554 //动画位移

#import "GameViewController.h"
#import "ASCView.h"

@implementation GameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

//    // create a new scene
//    SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/ship.scn"];
//
//    // create and add a camera to the scene
//    SCNNode *cameraNode = [SCNNode node];
//    cameraNode.camera = [SCNCamera camera];
//    [scene.rootNode addChildNode:cameraNode];
//    
//    // place the camera
//    cameraNode.position = SCNVector3Make(0, 0, 15);
//    
//    // create and add a light to the scene
//    SCNNode *lightNode = [SCNNode node];
//    lightNode.light = [SCNLight light];
//    lightNode.light.type = SCNLightTypeOmni;
//    lightNode.position = SCNVector3Make(0, 10, 10);
//    [scene.rootNode addChildNode:lightNode];
//    
//    // create and add an ambient light to the scene
//    SCNNode *ambientLightNode = [SCNNode node];
//    ambientLightNode.light = [SCNLight light];
//    ambientLightNode.light.type = SCNLightTypeAmbient;
//    ambientLightNode.light.color = [UIColor darkGrayColor];
//    [scene.rootNode addChildNode:ambientLightNode];
//    
//    // retrieve the ship node
//    SCNNode *ship = [scene.rootNode childNodeWithName:@"ship" recursively:YES];
//    
//    // animate the 3d object
//    [ship runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];
    
    // retrieve the SCNView
    ASCView *scnView = (ASCView *)self.view;
//    scnView.playing = YES;                          // Play the animations.
    scnView.autoenablesDefaultLighting = NO;       // Automatically light scenes that have no light.
    scnView.backgroundColor = [UIColor blackColor];
    
    // set the scene to the view
    // Load a scene
//    NSURL *url = [[NSBundle mainBundle] URLForResource:@"rose3" withExtension:@"dae"];
//    [scnView loadSceneAtURL:url];
    
    
    SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/rose3.scn"];
    scnView.scene=scene;
    
    //缩放下
    
    SCNNode * roseNode = [scene.rootNode childNodeWithName:@"GREE_01" recursively:YES];
    NSLog(@"缩放:%f,%f,%f",roseNode.scale.x,roseNode.scale.y,roseNode.scale.z);
    NSLog(@"位置:%f,%f,%f",roseNode.position.x,roseNode.position.y,roseNode.position.z);
    roseNode.scale=SCNVector3Make(9.0, 9.0, 9.0);//9.932703
    roseNode.position=SCNVector3Make(roseNode.position.x, roseNode.position.y+5.0, roseNode.position.z);//9.932703
    NSLog(@"修改后缩放:%f,%f,%f",roseNode.scale.x,roseNode.scale.y,roseNode.scale.z);
    NSLog(@"修改后位置:%f,%f,%f",roseNode.position.x,roseNode.position.y,roseNode.position.z);
    
    
    //[self printNodesName:scene.rootNode];
    
    // allows the user to manipulate the camera
    scnView.allowsCameraControl = YES;
        
    // show statistics such as fps and timing information
    scnView.showsStatistics = YES;


    
    // add a tap gesture recognizer
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    NSMutableArray *gestureRecognizers = [NSMutableArray array];
    [gestureRecognizers addObject:tapGesture];
    [gestureRecognizers addObjectsFromArray:scnView.gestureRecognizers];
    scnView.gestureRecognizers = gestureRecognizers;
}

- (void) handleTap:(UIGestureRecognizer*)gestureRecognize
{
    // retrieve the SCNView
    SCNView *scnView = (SCNView *)self.view;
    
    // check what nodes are tapped
    CGPoint p = [gestureRecognize locationInView:scnView];
    
//    ASCView *scnView = (ASCView *)self.view;
//    [scnView nodesInsideFrustumWithPointOfView:<#(nonnull SCNNode *)#>]
    
    NSArray *hitResults = [scnView hitTest:p options:nil];

    for (SCNHitTestResult * result in hitResults ) {
        //NSLog(@"select node:%@ ,g:%@",result.node.name,result.node.geometry.name);
      
        //如果点击头部,做动画
        if ([self checkIsTopNode:result.node]) {
            
            [self doHeadAction];
            
            break;
        }
        
        
    }

    

    
    

}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}



#pragma mark -----检测点击头部
-(BOOL)checkIsTopNode:(SCNNode*)node{
    NSString * name =node.name;
    name=CHECKSTR(name);
    
if ([name isEqualToString:@"pCylinder5"] || [name isEqualToString:@"polySurface1"] || [name isEqualToString:@"pCylinder4"] || [name isEqualToString:@"fengshan_1"] || [name isEqualToString:@"UP_W"]) {
    

    return YES;
}
    return NO;
}


//做动画
-(void)doHeadAction{

    SCNView *scnView = (SCNView *)self.view;
    SCNNode * rootNode =[scnView.scene rootNode];
    SCNNode * node=[rootNode childNodeWithName:@"UP_W" recursively:YES];
  
//    SCNAction * moveAction = [SCNAction customActionWithDuration: 1.0
//                                                     actionBlock:^(SCNNode * _Nonnull node, CGFloat elapsedTime) {
//                                                         
//                                                         node.position=SCNVector3Make(node.position.x, node.position.y-1.543, node.position.z);
//                                                     }];
    NSLog(@"before: %f ,%f ,%f",node.position.x,node.position.y,node.position.z);
    
    
    BOOL isNeedOpen = node.position.y<1.0 ? YES :NO;
    
    CGFloat moveY =  isNeedOpen? TRANSTRATEY : -TRANSTRATEY;
    
    
    SCNAction * moveAction = [SCNAction moveByX:0 y:moveY z:0 duration:0.5];
    
    [node runAction:moveAction completionHandler:^{
        
        NSLog(@"after: %f ,%f ,%f",node.position.x,node.position.y,node.position.z);
    }];
    
    

}


#pragma mark -----打印节点
-(void)printNodesName:(SCNNode*)node{

//    NSLog(@"-----parent node:%@",node.name);
    NSArray * cnodes=[node childNodes];
    
    for (SCNNode * n in cnodes) {
         NSLog(@"node:%@",n.name);
        [self printNodesName:n];
    }

}


@end
